/**
 * 2017-5-16 By Qing
 * Common's Functions
 * @param {@getdom} ele 
 */
// dom获取
export function getdom(ele) {
  if(ele == '') return;
  var ret;
  if(/([.])+/.test(ele)) {
    ret = document.querySelectorAll(ele);
  }else if(/([#])+/.test(ele)) {
    ret = document.querySelector(ele);
  }else if(/([.*])?/.test(ele)) {
    ret = document.querySelectorAll(ele);
  }
  return ret;
}
// 添加事件监听
export function addEvt(ele, event, callback) {
  if(ele) {
    return ele.addEventListener(event, callback, false);
  }
}
export function objExtends(base, son) {
	for(var p in son) {
		base[p] = son[p];
	}
	return base;
}
// 时间格式
export function parseTime(tm, cfg) {
	var baseConfig = {
		'mode': 'static', // 'static'/'dynamic'
		'format': 'YY-MM-DD h:m:s d',
		'htm': 'body'
	};
	tm = Number(tm);
  cfg = (cfg === undefined || cfg === null || ((typeof cfg) != 'object')) ? baseConfig : objExtends(baseConfig, cfg);
  console.log('40,,,,,,,,,,,', cfg);
	// 1.Get original Time
	var t, time = '';
	t = !tm ? new Date() : new Date(tm);
	var YY = t.getFullYear(),
		$m = t.getMonth()+1,
		MM = ad0($m),
		$d = t.getDate(),
		DD = ad0($d),
		d = weekTrans(t.getDay()),
		$h = t.getHours(),
		h = ad0($h),
		$m = t.getMinutes(),
		m = ad0($m),
		$s = t.getSeconds(),
		s = ad0($s);
	if(cfg.mode == 'static') {
		switch(cfg.format) {
			case 'YY-MM-DD': 
				time = YY +'-'+ MM +'-'+ DD;
        break;
      case 'y.m.d':
        time = `${YY}.${MM}.${DD}`;
        break;
			case 'h:m:s':
				time = h+':'+m+':'+s;
				break;
			case 'YY-MM-DD h:m:s':
				time = YY +'-'+ MM +'-'+ DD +' '+h+':'+m+':'+s;
				break;
			default:
				time = YY +'-'+ MM +'-'+ DD +' '+h+':'+m+':'+s+' '+d;
		}
		return time;
	}else if(cfg.mode == 'dynamic') {
		var timer = null, curH = '';
		timer = setInterval(function() {
			$s++;
			if($s >=60) {
				$m++;
				$s = 0;
			}
			if($m >= 60) {
				$h++;
				$m = 0;
			}
			if($h >= 24) {
				$h = 0;
			}
			curH = ad0($h) +':'+ ad0($m) +':'+ ad0($s);
			$v.getdom(cfg.htm)[0].innerText = curH;
		}, 1000);
	}
	// Handle week
	function weekTrans(day) {
		switch(day) {
			case 1:
				return '星期一';
				break;
			case 2:
				return '星期二';
				break;
			case 3:
				return '星期三';
				break;
			case 4:
				return '星期四';
				break;
			case 5:
				return '星期五';
				break;
			case 6:
				return '星期六';
				break;
			default:
				return '星期日';
				break;
		}
	}
	// Additional '0'
	function ad0(val) {
		return val < 10 ? ('0' + val) : val;
	}
	
	return time;
}